import asyncio, time


async def do_work(m):
    print(f"this is do_work: {m}")
    await asyncio.sleep(1)
    print(f"this is do_work: {m} end")
    return f"do_work done: {m}"


async def main():
    start = time.time()
    print(f"start: {time.time()}")
    co_objects = [do_work(i) for i in range(3)]
    print(co_objects)

    loop = asyncio.get_running_loop()
    print(loop)

    # tasks = [asyncio.create_task(co) for co in co_objects]
    # print(tasks)
    # # print(asyncio.all_tasks())

    # await tasks[0]
    # time.sleep(10)

    # 第一种并行方式
    # result = await asyncio.gather(*co_objects)
    # print(result)

    # 第二种并行方式
    tasks = [asyncio.create_task(co) for co in co_objects]
    dones, _ = await asyncio.wait(tasks)
    for res in dones:
        print(res.result())

    print(f"end: {time.time() - start}")
    print(asyncio.all_tasks())


asyncio.run(main())
